﻿using DG.Tweening;
using HighlightingSystem;
using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UIMain : MonoBehaviour
{
    public static UIMain Instance;

    //[Header("正常状态下的UI")]
    //public List<GameObject> Stove_3D_Normal;

    //[Header("正常状态下的3DUI(截图专用UI)")]
    //public List<GameObject> Stove_3DJT_Normal;

    //[Header("报警状态下的UI")]
    //public List<GameObject> Stove_3D_Warn;

    //[Header("报警状态下的3DUI(截图专用UI)")]
    //public List<GameObject> Stove_3DJT_Warm;

   

    //[Header("报警添加的2DUI")]
    //public List<GameObject> Stove_2D_Warn;

    //[Header("报警UI2D时间文本")]
    //public List<TextMeshProUGUI> Stove_timetxt;

    //[Header("报警中心个数")]
    //public TextMeshProUGUI WarnCenter;
    //[Header("统计面板报警文本")]
    //public TextMeshProUGUI Statistics_num;

    //public Transform alertUI;
    //public Toggle alertTog;

    #region 3D物体UI屏幕坐标所需 
    internal static Camera uiCamera;
    internal Vector2 localPoint;
    internal Vector2 oldLocalPoint;
    internal Vector3 playerScreenPos;
    #endregion
    private void Awake()
    {
        if (Instance == null)
        {
            //Debug.Log("aaa");
            Instance = this;
        }
    }

    void Start()
    {
        uiCamera = transform.Find("UICamera")?.GetComponent<Camera>();
        //StartCoroutine(AlertMain());
    }

    private void FixedUpdate()
    {
        
    }

    //update调用
    public void SetUIRectangl(Transform point_Transf, Transform ui_Transf, RectTransform uiParent)
    {
        //物体坐标转为屏幕坐标
        playerScreenPos = Camera.main.WorldToScreenPoint(point_Transf.position);
        //Debug.Log(ui_Transf.localScale);
        if (ui_Transf.localScale != Vector3.zero && playerScreenPos.z < 0)
        {
            //ui_Transf.GetComponent<Animator>().enabled = false;
            ui_Transf.localScale = Vector3.zero;
        }
        else if (ui_Transf.localScale != Vector3.one && playerScreenPos.z > 0)
        {
            //ui_Transf.GetComponent<Animator>().enabled = true;
            ui_Transf.localScale = Vector3.one;
        }
        //Debug.Log("屏幕坐标" + playerScreenPos);
        //屏幕坐标转UI坐标
        RectTransformUtility.ScreenPointToLocalPointInRectangle(uiParent, new Vector2(playerScreenPos.x, playerScreenPos.y), uiCamera, out localPoint);
        //Debug.Log(localPoint);
        if ((Math.Abs(localPoint.x - oldLocalPoint.x) > 1.5) || (Math.Abs(localPoint.y - oldLocalPoint.y) > 1.5))//防抖动
        {
            oldLocalPoint = localPoint;
            this.GetComponent<RectTransform>().anchoredPosition3D = localPoint;
        }
    }


    

    public void CloseAlertUI()
    {
        //StartCoroutine(MotionManager.instance.MoveObj_IE(alertUI, UIRight.instance.endPoint_V3, 25f, () =>
        //{

        //}));
    }


    #region 告警演示版
    /*
     * 告警每5分钟提醒一次（三个告警点位（设备）一号炉，二号炉，三号炉）
     * 告警后界面弹出告警提示窗口（窗口动效）
     * 点击立即前往则相机直接移动到设备位置
     * 告警设备描边；3DUI切换成报警UI
     * 
     *      告警列表
     *  设备告警后列表新增该设备的告警信息
     *  搜索框字符匹配设备名，模糊搜索
     */

    public double alertInterval_double = 10;//告警间隔时长（秒）**测试改为10s

    IEnumerator AlertMain()
    {
        int i = 0;//三台设备报警类型区分
        bool timeON;
        yield return null;
        while (true)
        {
            DateTime startTime = DateTime.Now;
            timeON = true;
            yield return new WaitForFixedUpdate();
            while (timeON)
            {
                yield return new WaitForFixedUpdate();
                if ((DateTime.Now - startTime).TotalSeconds >= alertInterval_double)
                {
                    startTime = DateTime.Now;
                    i++;
                    if (i == 1)
                    {
                        //一号炉设备报警
                        //Warn(0, Stove_3D_Normal, Stove_3DJT_Normal,Stove_3D_Warn, Stove_3DJT_Warm, WarmEvent.instance.Stove1, Stove_2D_Warn, Stove_timetxt, WarnCenter, Statistics_num);//设备报警
                    }
                    if (i == 2)
                    {
                        //二号炉设备报警
                        //Warn(1, Stove_3D_Normal, Stove_3DJT_Normal,Stove_3D_Warn, Stove_3DJT_Warm, WarmEvent.instance.Stove1, Stove_2D_Warn, Stove_timetxt, WarnCenter, Statistics_num);
                    }
                    if (i == 3)
                    {
                        //三号炉子设备报警
                        //Warn(2, Stove_3D_Normal, Stove_3DJT_Normal,Stove_3D_Warn, Stove_3DJT_Warm, WarmEvent.instance.Stove1, Stove_2D_Warn, Stove_timetxt, WarnCenter, Statistics_num);
                        i = 1;
                        //timeON = false;//第三台设备报警，不再进入循环
                    }
                    Debug.Log("设备告警");
                }
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="Stove_3D_Normal">正常状态下的UI</param>
    /// <param name="Stove_3D_Warn">报警状态下的UI</param>
    /// <param name="Stove1">产生报警的设备</param>
    /// <param name="Stove_2D_Warn">报警添加的2DUI</param>
    /// <param name="Stove_timetxt">报警UI2D时间文本</param>
    public void Warn(int i, List<GameObject> Stove_3D_Normal,List<GameObject> Stove_3DJT_Normal, List<GameObject> Stove_3D_Warn, List<GameObject> Stove_3DJT_Warm,  List<Highlighter> Stove1, List<GameObject> Stove_2D_Warn, List<TextMeshProUGUI> Stove_timetxt, TextMeshProUGUI Warncenter, TextMeshProUGUI Statistics_num)//设备报警
    {
        Statistics_num.text = (i + 1).ToString();
        Warncenter.text = (i + 1).ToString();
        Stove_3D_Normal[i].SetActive(false);
        Stove_3DJT_Normal[i].SetActive(false);
        Stove_3D_Warn[i].SetActive(true);
        Stove_3DJT_Warm[i].SetActive(true);
        Stove1[i].constant = true;
        Stove_2D_Warn[i].SetActive(true);
        Stove_timetxt[i].text = DateTime.Now.ToString().Replace("/", ".");
    }

    #endregion

    //public Transform jietuCamer;
    //Coroutine coroutine;
    //public void 截图(Transform alertTrs)
    //{
    //    if (coroutine != null)
    //        StopCoroutine(coroutine);
    //    jietuCamer.gameObject.SetActive(true);
    //    jietuCamer.transform.position = alertTrs.position;
    //    jietuCamer.transform.eulerAngles = alertTrs.eulerAngles;
    //    jietuCamer.GetComponent<Capture>().fn_shot();

    //    coroutine = StartCoroutine(Ing(jietuCamer.gameObject));
    //}
    //IEnumerator Ing(GameObject cam) 
    //{
    //    Debug.Log("协程");
    //    DateTime dateTime = DateTime.Now;
    //    while (true)
    //    {
    //        yield return new WaitForFixedUpdate();
    //        while ((DateTime.Now - dateTime).TotalSeconds>=1) 
    //        {
    //            cam.SetActive(false);
    //            yield break;
    //        }
    //    }
    //}

    //public void 移动至告警点位(Transform alertTrsf)
    //{
    //    UIDown.instance.第一视角_Tog.isOn = true;

    //    MotionManager.instance.MoveObj(Camera.main.transform.parent, alertTrsf.position, 3f, () =>
    //    {
    //        Debug.Log("已移动到选中产线");
    //    });
    //    Vector3 vector3 = new Vector3(0, alertTrsf.localEulerAngles.y, 0);
    //    Camera.main.transform.parent.DOLocalRotateQuaternion(Quaternion.Euler(new Vector3(0, alertTrsf.localEulerAngles.y, 0)), 1f);
    //    Camera.main.transform.DOLocalRotateQuaternion(Quaternion.Euler(new Vector3(alertTrsf.localEulerAngles.x, 0, 0)), 1f);
    //}

}
